package com.example.wangyi.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment<P extends BasePresenter,T> extends Fragment implements BaseView<T> {
    private Unbinder mBind;
    private P mPersenter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int layoutId = getLayoutId();
        View view = null;
        if (layoutId != 0) {
            view = inflater.inflate(layoutId, null);
            mBind = ButterKnife.bind(this, view);
        }
        mPersenter = createPresenter();
        if (mPersenter!=null)
            mPersenter.attachView(this);
        return view;
    }

    protected abstract P createPresenter();
    protected P getmPresenter(){
        if (mPersenter!=null)
            return mPersenter;
        return null;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
        initData();

    }



    protected abstract void initData();

    protected abstract void init();

    protected abstract int getLayoutId();

    @Override
    public void onSuccess(T t) {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mBind != null)
            mBind.unbind();
        if (mPersenter!=null){
            mPersenter.dettachView();
            mPersenter = null;
        }
    }
}